package com.lhd.web;

import com.lhd.po.Blog;
import com.lhd.po.Tag;
import com.lhd.po.Type;
import com.lhd.service.BlogService;
import com.lhd.service.TagService;
import com.lhd.service.TypeService;
import com.lhd.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.List;

/**
 * Created by limi on 2017/10/13.
 */
@Controller
public class IndexController {

    @Autowired
    private BlogService blogService;

    @Autowired
    private TypeService typeService;

    @Autowired
    private TagService tagService;

    @Autowired
    private UserService userService;

    @GetMapping("/")
    public String index(@RequestParam(value = "page", required = false, defaultValue = "0") Integer page, Model model) {
        List<Blog> blogs = blogService.listBlog(page);
        for (Blog b:blogs){
            b.setUser(userService.getUserById(b.getUserId()));
            b.setType(typeService.getType(b.getTypeId()));
        }
        model.addAttribute("page",blogs);
        List<Type> types = typeService.listTypeTop(6);
        for (Type type:types){
            type.setCount(typeService.countTypeByTypeId(type.getId()));
        }
        model.addAttribute("types", types);
        List<Tag> tags = tagService.listTagTop(10);
        for (Tag tag:tags){
            tag.setCount(tagService.getCountByTagId(tag.getId()));
        }
        model.addAttribute("tags", tags);
        model.addAttribute("recommendBlogs", blogService.listRecommendBlogTop(8));
        model.addAttribute("totalElements",blogs.size());
        Long pageNum = blogService.countBlog();
        Long rem = pageNum % 10;
        model.addAttribute("page",blogs);
        model.addAttribute("pageNum",pageNum/10 + (rem == 0 ? 0 : 1));
        model.addAttribute("curPage",page);
        model.addAttribute("preBoolean",page != 0);
        model.addAttribute("nexBoolean",page != ((pageNum/10 + (rem == 0 ? 0 : 1))-1));
        return "index";
    }


    @PostMapping("/search")
    public String search(@RequestParam(value = "page", required = false, defaultValue = "0") Integer page,
                         @RequestParam String query, Model model) {
        model.addAttribute("page", blogService.listBlog("%"+query+"%", page));
        model.addAttribute("query", query);
        return "search";
    }

    @GetMapping("/blog/{id}")
    public String blog(@PathVariable Long id,Model model) {
        Blog blog = blogService.getAndConvert(id);
        blog.setType(typeService.getType(blog.getTypeId()));
        blog.setUser(userService.getUserById(blog.getUserId()));
        if (blog.getViews() == null){
            blog.setViews(1);
        }else {
            blog.setViews(blog.getViews()+1);
        }
        blogService.updateBlog(blog.getId(),blog);
        model.addAttribute("blog", blog);
        return "blog";
    }

    @GetMapping("/footer/newblog")
    public String newblogs(Model model) {
        model.addAttribute("newblogs", blogService.listRecommendBlogTop(3));
        return "_fragments :: newblogList";
    }

}
